package com.example.mobile2021606176mhq0513;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;

import com.example.mobile2021606176mhq0513.fragment.ETCFragment;
import com.example.mobile2021606176mhq0513.fragment.EnviFragment;
import com.example.mobile2021606176mhq0513.fragment.IndexFragment;
import com.example.mobile2021606176mhq0513.fragment.LightFragment;
import com.google.android.material.navigation.NavigationView;

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private DrawerLayout drawerLayout;
    private FrameLayout frameLayout;
    private NavigationView navigation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name) {
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }

            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }

        };
        toggle.syncState();
        drawerLayout.addDrawerListener(toggle);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.frameLayout, IndexFragment.newInstance("", ""), "")
                .commit();
        toolbar.setTitle("智能交通系统");

        navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                item.setCheckable(true);
                switch (item.getItemId()) {
                    case R.id.itemindex:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frameLayout, IndexFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("智能交通系统");
                        break;
                    case R.id.itemETC:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frameLayout, ETCFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("ETC账户");
                        break;
                    case R.id.itemEnvi:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frameLayout, EnviFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("环境指标");
                        break;
                    case R.id.itemLight:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.frameLayout, LightFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("交通灯");
                        break;
                    case R.id.itemExit:
                        MainActivity.this.finish();
                        break;


                }

                drawerLayout.closeDrawers();
                return true;


            }
        });
    }


            private void initView() {
                toolbar = (Toolbar) findViewById(R.id.toolbar);
                drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
                frameLayout = (FrameLayout) findViewById(R.id.frameLayout);
                navigation = (NavigationView) findViewById(R.id.navigation);
            }
        }